enum NUM {
    //% block="1"
    0,
    //% block="2"
    1,
    //% block="3"
    2,
    //% block="4"
    3,
    //% block="5"
    4,
    //% block="6"
    5,
    //% block="7"
    6,
    //% block="8"
    7 
}
enum BTN {
    //% block="上"
    0,
    //% block="下"
    1,
    //% block="左"
    2,
    //% block="右"
    3,
    //% block="F2"
    4,
    //% block="F4"
    5,
    //% block="F6"
    6
}
enum Mnum {
    //% block="M1(D4,D5)"
    0,
    //% block="M2(D6,D7)"
    1
}
enum Mdir {
    //% block="前进"
    0,
    //% block="后退"
    1
}
//% color="#AA278D" iconWidth=50 iconHeight=40
namespace GeneralUNO {

    //% block="TB6612/L293/L298 设置电机 [MOTOR] 以[SPEED]速度[DIR]" blockType="command"
    //% MOTOR.shadow="dropdown" MOTOR.options="Mnum"  MOTOR.defl="Mnum.0"
    //% DIR.shadow="dropdown" DIR.options="Mdir"  DIR.defl="Mdir.0"
    //% SPEED.shadow="number" SPEED.defl=255
    export function tb6612_uno(parameter: any, block: any) {
        let pin = parameter.MOTOR.code;
        let dir = parameter.DIR.code;
        let speed = parameter.SPEED.code;
        var M_CODE=[["4","5"],["7","6"]];
        Generator.addObject(`tb6612`, ` `, `void setMotor_tb6612(int speedpin,int dirpin,int dir,int speed)\n\t
   {
     if (dir > 0)
      {
        digitalWrite(dirpin, dir);
        analogWrite(speedpin, speed);
      }
    else
     {
        digitalWrite(dirpin, dir);
        analogWrite(speedpin, speed);
     }
}`);
        Generator.addSetup(`${pin}_Setup`, 
 `pinMode(${M_CODE[pin][0]}, OUTPUT);
  pinMode(${M_CODE[pin][1]}, OUTPUT);
  digitalWrite(${M_CODE[pin][0]}, LOW);
  digitalWrite(${M_CODE[pin][1]}, LOW);`);
        Generator.addCode(`setMotor_tb6612(${M_CODE[pin][1]}, ${M_CODE[pin][0]},${dir},${speed});`);
    }

    //% block="L9110/DRV8833 设置电机 [MOTOR] 以[SPEED]速度[DIR]" blockType="command"
    //% MOTOR.shadow="dropdown" MOTOR.options="Mnum"  MOTOR.defl="Mnum.0"
    //% DIR.shadow="dropdown" DIR.options="Mdir"  DIR.defl="Mdir.0"
    //% SPEED.shadow="number" SPEED.defl=255
    export function l9110s_uno(parameter: any, block: any) {
        let pin = parameter.MOTOR.code;
        let dir = parameter.DIR.code;
        let speed = parameter.SPEED.code;
        var M_CODE=[["4","5"],["7","6"]];
        Generator.addObject(`l9110s`, ` `, `void setMotor_l9110s(int speedpin,int dirpin,int dir,int speed)\n\t
   {
     if (dir > 0)
     {
        digitalWrite(dirpin, dir);
        analogWrite(speedpin, 255-speed);
     }
     else
     {

        digitalWrite(dirpin, dir);
        analogWrite(speedpin, speed);      
     }
}`);
        Generator.addSetup(`${pin}_Setup`, 
 `pinMode(${M_CODE[pin][0]}, OUTPUT);
  pinMode(${M_CODE[pin][1]}, OUTPUT);
  digitalWrite(${M_CODE[pin][0]}, LOW);
  digitalWrite(${M_CODE[pin][1]}, LOW);`);
        Generator.addCode(`setMotor_l9110s(${M_CODE[pin][1]}, ${M_CODE[pin][0]},${dir},${speed});`);
    }

    //% block="读取引脚[PIN_DigitalWrite] 红外接收值" blockType="reporter"
    //% PIN_DigitalWrite.shadow="dropdown" PIN_DigitalWrite.options="PIN_DigitalWrite"  PIN_DigitalWrite.defl="PIN_DigitalWrite.1"
    export function remote_uno(parameter: any, block: any) {
          let rev_pin = parameter.PIN_DigitalWrite.code
        Generator.addInclude('vs1838b_include', '#include <IRremote.h>');
        Generator.addObject(`vs1838b_define`, ` `, `IRrecv irrecv_${rev_pin}(${rev_pin});\n\tdecode_results results_${rev_pin};\n\t`);
        Generator.addObject(`vs1838b_define_${rev_pin}`, ` `, `long remote_${rev_pin}()
{
   if (irrecv_${rev_pin}.decode(&results_${rev_pin})) {
    long rev = results_${rev_pin}.value;
    irrecv_${rev_pin}.resume();
    return rev;
  }
  else{ return 0; }
}`);
        Generator.addSetup(`vs1838b_${rev_pin}_begin`,`irrecv_${rev_pin}.enableIRIn();`);
        Generator.addCode(`remote_${rev_pin}()`);
    }

    //% block="遥控器通道[PART] - [BUTTON]红外值" blockType="reporter"
    //% PART.shadow="dropdown" PART.options="NUM"  PART.defl="NUM.0"
    //% BUTTON.shadow="dropdown" BUTTON.options="BTN"  BUTTON.defl="BTN.0"
    export function handuan_uno(parameter: any, block: any) {
        let part = parameter.PART.code;
        let button = parameter.BUTTON.code;
        var REV_CODE=[["370326124","-401798257","722094923","501778864","87667472","1457459944","1189162337"],
                 ["-2060198430","1954985285","781883471","835149340","2113464299","653646598","855791861"],
                 ["-129486175","30903566","-1148828784","-1535736237","257421278","1277065657","-1068289858"],
                 ["-79153320","47681187","-1199161639","-1552513858","274198899","1327398512","-1051512237"],
                 ["-79153317","47681184","-1199161638","-1552513859","274198896","1327398515","-1051512240"],
                 ["-750405174","718933041","1216271043","327020756","945450753","656146658","-380260383"],
                 ["-1811806114","1780333981","1618831895","-1680353374","402038415","1199558996","-923672721"],
                 ["397148545","-1235920999","-1552513858","-112027819","274198899","274198899","1680019374"]];
        Generator.addCode(`"${REV_CODE[part][button]}"`);
    }
}
